package com.wzh.himalaya.presenters;

import androidx.annotation.Nullable;

import com.wzh.himalaya.interfaces.IRecommendPresenter;
import com.wzh.himalaya.interfaces.IRecommendViewCallback;
import com.wzh.himalaya.utils.Constants;
import com.wzh.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.AlbumList;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * className:RecommendPresenter
 * Author:WZH
 * Time:2022-02-22
 */
public class RecommendPresenter implements IRecommendPresenter {

    private static final String TAG = "RecommendPresenter";
    private List<IRecommendViewCallback> mCallbacks = new ArrayList<>();

    private RecommendPresenter(){}
    private static RecommendPresenter sInstance =null;

    /**
     * 获取单例对象
     * @return
     */
    public static RecommendPresenter getInstance(){
        if (sInstance == null) {
            synchronized (RecommendPresenter.class){
                if (sInstance == null) {
                    sInstance  =new RecommendPresenter();
                }
            }
        }
        return  sInstance;
    }

    /**
     * 获取推荐内容
     * 接口3.10.6 获取猜你喜欢专辑
     */
    @Override
    public void getRecommendList() {
        //获取推荐内容
        //封装参数
        updateLoading();

        Map<String, String> map = new HashMap<>();

        //这个参数表示一页数据返回多少条
        map.put(DTransferConstants.LIKE_COUNT, Constants.RECOMMEND_RECOMMEND + "");
        CommonRequest.getGuessLikeAlbum(map, new IDataCallBack<GussLikeAlbumList>() {
            @Override
            public void onSuccess(@Nullable GussLikeAlbumList gussLikeAlbumList) {
                //数据获取成功
                if (gussLikeAlbumList != null) {
                    List<Album> albumList = gussLikeAlbumList.getAlbumList();
                    //数据回来之后我们要去更新UI
                    //       upRecommendUI(albumList);
                    handlerRecommendResult(albumList);
                }


            }


            @Override
            public void onError(int i, String s) {
                //数据获取出错
                LogUtil.d(TAG, "error---->" + i);
                LogUtil.d(TAG, "errorMsg---->" + s);
                handlerError();
            }
        });
    }

    private void handlerError() {

        //通知UI更新
        if (mCallbacks != null) {
            for (IRecommendViewCallback callback : mCallbacks) {
                callback.onNetworkError();
            }
        }
    }


    private void handlerRecommendResult(List<Album> albumList) {

        if (albumList != null) {
            if (albumList.size()==0) {
                for (IRecommendViewCallback callback : mCallbacks) {
                    callback.onEmpty();//空数据
                }
            }else {//处理成功
                for (IRecommendViewCallback callback : mCallbacks) {
                    //通知UI更新
                    callback.onRecommendListLoaded(albumList);
                }
            }
        }
    }


    private void updateLoading() {
        for (IRecommendViewCallback callback : mCallbacks) {
            callback.onLoading();
        }
    }

    @Override
    public void pull2RefreshMore() {

    }

    @Override
    public void loadMore() {

    }

    @Override
    public void registerViewCallback(IRecommendViewCallback callback) {
       if (mCallbacks!=null && !mCallbacks.contains(callback))
       {
           mCallbacks.add(callback);
       }
    }

    @Override
    public void unregisterViewCallback(IRecommendViewCallback callback) {
        mCallbacks.remove(mCallbacks);
    }
}
